package com.example.maili.woxiamg.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.maili.woxiamg.R;
import com.example.maili.woxiamg.adapter.homeadapter.HomeTypeAdapter;
import com.example.maili.woxiamg.api.API;
import com.example.maili.woxiamg.api.APIS;
import com.example.maili.woxiamg.bean.HomeBanner;
import com.example.maili.woxiamg.bean.HomeBean;
import com.example.maili.woxiamg.bean.HomeDataBean;
import com.example.maili.woxiamg.utils.AppUtils;
import com.example.maili.woxiamg.utils.OkHttpClientManager;
import com.example.maili.woxiamg.view.FullyLinearLayoutManager;
import com.example.maili.woxiamg.view.GlideImageLoader;
import com.google.gson.Gson;
import com.jwenfeng.library.pulltorefresh.BaseRefreshListener;
import com.jwenfeng.library.pulltorefresh.PullToRefreshLayout;
import com.mikhaellopez.circularimageview.CircularImageView;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Request;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends Fragment {

    private View inflate;
    private Banner banner_Home;
    private RecyclerView recycler_List;
    private PullToRefreshLayout pullToRefreshLayout;
    private TextView tv_SearchBg;
    private TextView tv_Title;
    private CircularImageView img_Login;
    private List<HomeDataBean> data = new ArrayList<>();
    private HomeTypeAdapter adapter;
    private FullyLinearLayoutManager layoutManager;
    private List<String> imgUrl = new ArrayList<>();

    public HomeFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        inflate = inflater.inflate(R.layout.fragment_home, container, false);
        initView(inflate);
        getData();
        return inflate;
    }

    /**
     * 获取主页数据
     */
    private void getData() {
        OkHttpClientManager.getAsyn(API.APP_INDEX, new OkHttpClientManager.ResultCallback<String>() {
            @Override
            public void onError(Request request, Exception e) {
                AppUtils.toast(getContext(), "网络请求失败");
            }

            @Override
            public void onResponse(String response) {
                Gson getgson = AppUtils.getgson();
                HomeBean homeBean = getgson.fromJson(response, HomeBean.class);
                data.add(new HomeDataBean(HomeDataBean.NEWS, homeBean.getNewsList(), null, null, null));
                data.add(new HomeDataBean(HomeDataBean.STORY, null, null, homeBean.getStoryList(), null));
                data.add(new HomeDataBean(HomeDataBean.SCRIPT, null, homeBean.getScriptList(), null, null));
                data.add(new HomeDataBean(HomeDataBean.VIDEO, null, null, null, homeBean.getVideoList()));
                adapter.notifyDataSetChanged();
            }
        });
        OkHttpClientManager.getAsyn(APIS.LOAD_BANNER, new OkHttpClientManager.ResultCallback<String>() {
            @Override
            public void onError(Request request, Exception e) {
                AppUtils.toast(getContext(), "网络请求失败");
            }

            @Override
            public void onResponse(String response) {
                imgUrl.clear();
                Gson gson = AppUtils.getgson();
                HomeBanner homeBanner = gson.fromJson(response, HomeBanner.class);
                List<HomeBanner.BannerListBean> bannerList = homeBanner.getBannerList();
                for (int i = 0; i < bannerList.size(); i++) {
                    imgUrl.add(bannerList.get(i).getImage());
                }
                setBanner();
            }
        });
    }

    private void initView(View inflate) {
        banner_Home = (Banner) inflate.findViewById(R.id.banner_Home);
        recycler_List = (RecyclerView) inflate.findViewById(R.id.recycler_List);
        pullToRefreshLayout = (PullToRefreshLayout) inflate.findViewById(R.id.pullToRefreshLayout);
        tv_SearchBg = (TextView) inflate.findViewById(R.id.tv_SearchBg);
        tv_Title = (TextView) inflate.findViewById(R.id.tv_Title);
        img_Login = (CircularImageView) inflate.findViewById(R.id.img_Login);
        //设置布局管理器
        layoutManager = new FullyLinearLayoutManager(getContext());
        recycler_List.setLayoutManager(layoutManager);
        adapter = new HomeTypeAdapter(data, getContext());
        recycler_List.setAdapter(adapter);
        pullToRefreshLayout.setRefreshListener(new BaseRefreshListener() {
            @Override
            public void refresh() {
                pullToRefreshLayout.finishRefresh();
            }

            @Override
            public void loadMore() {
                pullToRefreshLayout.finishLoadMore();
            }
        });
    }

    /**
     * 轮播图
     */
    private void setBanner() {
        banner_Home.setImages(imgUrl);
        banner_Home.setImageLoader(new GlideImageLoader());
        banner_Home.start();
        banner_Home.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
//                Intent intent = new Intent(getContext(), ReadStoryActivity.class);
//                intent.putExtra("url", Long.valueOf(imageUrlList.get(position).getId()));
//                startActivity(intent);
            }
        });
    }
}
